var ywcb = {
    setBtnStatus: function () {
        var row = $('#ywcb-grid').datagrid('getSelected');
        if (row && row.id) {
            //未导入
            if (row.CZZT == 'wdr') {
                $("#btn_ywcb_insert").show();//导入
                $("#btn_ywcb_cx").hide();//撤销
                $("#btn_ywcb_cz").hide();//重置
            }
            if (row.CZZT == 'drcg') {
                $("#btn_ywcb_insert").hide();//导入
                $("#btn_ywcb_cx").show();//撤销
                $("#btn_ywcb_cz").hide();//重置
            }
            if (row.CZZT == 'drsb') {
                $("#btn_ywcb_insert").hide();//导入
                $("#btn_ywcb_cx").hide();//撤销
                $("#btn_ywcb_cz").show();//重置
            }
            if (row.CZZT == 'sccg') {
                $("#btn_ywcb_insert").show();//导入
                $("#btn_ywcb_cx").hide();//撤销
                $("#btn_ywcb_cz").hide();//重置
            }
            if (row.CZZT == 'scsb') {
                $("#btn_ywcb_insert").hide();//导入
                $("#btn_ywcb_cx").hide();//撤销
                $("#btn_ywcb_cz").show();//重置
            }
        } else {
            $("#btn_ywcb_insert").hide();//导入
            $("#btn_ywcb_cx").hide();//撤销
            $("#btn_ywcb_cz").hide();//重置
        }
    }
}

$(function () {
    ywcb_init();

    // 导入凭证按钮
    $("#btn_ywcb_insert").click(function () {
        ywcb_insert();
    });
    //撤销
    $("#btn_ywcb_cx").click(function () {
        ywcb_cx();
    });
    //重置
    $("#btn_ywcb_cz").click(function () {
        ywcb_cz();
    });

})

//弹票据贴现窗口
function ywcb_insert() {
    var row = $('#ywcb-grid').datagrid('getSelected');
    if(row){
        var win = $.window({
            title:"票据贴现承兑",
            width:1000,
            height:300,
            cache:false,
            url:$.webPath() + "/cwgl/ywcb/ywcb_insert?ywcbId="+row.id,
            modal:true,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $(win).mask();
                        $.ajax({
                            url:$.webPath() + "/cwgl/ywcb/generate_voucher",
                            dataType: 'json',
                            type: 'POST',
                            data:{"ywcbSystemDate":$("#ywcb_systemDate").datebox("getValue"),
                                "attachment":$("#ywcb_attachment").val(),
                                "bz":$("#ywcb_bz").val(),
                                "ywcbId":$("#ywcb_id").val()},
                            beforeSend: function () {
                                if($("#ywcb_attachment").val()=="" || $("#ywcb_attachment").val()==null){
                                    $.messager.alert("提示","请填写附单据数", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                                if($("#ywcb_systemDate").datebox("getValue")=="" || $("#ywcb_systemDate").datebox("getValue")==null){
                                    $.messager.alert("提示","请填写制单时间", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                            },
                            success:function(msg){
                                $(win).mask('hide');
                                $(win).dialog('close');
                                $("#layout_ywcb").mask("hide");
                                if (msg.responseflag == 0){
                                    $('#ywcb-grid').datagrid('reload');
                                    $.messager.alert("提示", msg.responsemessage, "error");
                                }else {
                                    $('#ywcb-grid').datagrid('reload');
                                    $.messager.show({
                                        title: '提示',
                                        msg: msg.responsemessage,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                }
                            }
                        });
                    }
                }
            ]
        });
    }else{
        $.messager.alert('提示', '请选择一行数据', 'error');
    }
}

//贴现承兑撤销
function ywcb_cx(){
    var row = $('#ywcb-grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否撤销该贴现承兑？', function (r) {
            if (r) {
                var index = $('#ywcb-grid').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/ywcb/revoke_voucher?ywcbId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_ywcb").mask();
                    },
                    success:function(msg){
                        $("#layout_ywcb").mask("hide");
                        if (msg.responseflag == 0){
                            $('#ywcb-grid').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        } else {
                            $('#ywcb-grid').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要撤销的票据！", "warning");
    }
}

//贴现承兑重置
function ywcb_cz(){
    var row = $('#ywcb-grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否重置该票据？', function (r) {
            if (r) {
                var index = $('#ywcb-grid').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/ywcb/rest_voucher?ywcbId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_ywcb").mask();
                    },
                    success:function(msg){
                        $("#layout_ywcb").mask("hide");
                        if (msg.responseflag == 0){
                            $('#ywcb-grid').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        } else {
                            $('#ywcb-grid').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要重置的票据！", "warning");
    }
}


function ywcb_init() {

    $("[name='ywcb_sbgs']").jgbmtree($("[name='ywcb_sbbm']"), $("[name='ywcb_sbr']"), '', '')
    //客户加载
    $("[name='ywcb_kh']").qyjgzcombobox();
    //工程加载
    $("[name='ywcb_gc']").cxgccombobox();
    var options = getYwbcGridOption();
    $('#ywcb-grid').datagrid(options);
}

function ywcb_search() {
    var zt = $("#ywcb-toolbar input[name='ywcb_zt']").val();
    var xsdh = $("#ywcb-toolbar input[name='ywcb_xsdh']").val();
    var kh = $("#ywcb-toolbar input[name='ywcb_kh']").val();
    var sbgs = $("#ywcb-toolbar input[name='ywcb_sbgs']").val();
    var sbbm = $("#ywcb-toolbar input[name='ywcb_sbbm']").val();
    var sbr = $("#ywcb-toolbar input[name='ywcb_sbr']").val();
    var gc = $("#ywcb-toolbar input[name='ywcb_gc']").val();
    var fhsjStart = $('#fhrq_start').datebox('getValue');
    var fhsjEnd = $('#fhrq_end').datebox('getValue');
    var options = $.extend(getYwbcGridOption(),
        {pageNumber: 1, queryParams: {zt: zt, xsdh: xsdh, khId: kh, sbgsId: sbgs, sbbmId: sbbm, sbrId: sbr, gcId: gc, fhsjStart: fhsjStart, fhsjEnd: fhsjEnd}});
    $('#ywcb-grid').datagrid(options);
}




function getYwbcGridOption() {
    var options = {
        url: $.webPath() + "/cwgl/ywcb/ywcbList?sftzd=false",
        toolbar: "#ywcb-toolbar",
        fitColumns: true,
        singleSelect: true,
        pageNumber: 1,
        pagination: true,
        fit: true,
        rownumbers: true,
        showFooter: true,
        onLoadSuccess: function (target) {
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow:function(){
        ywcb.setBtnStatus();
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.xslx) {
                    this.xslx = this.xslx == 'htxs' ? "合同销售" : this.xslx == 'zddh' ? "直达到货" : this.xslx == 'bgxs' ? "保供销售" : "库存零售";
                }
            })
            return data;
        }
    }

    var columns = {columns: [
        [
            {field: 'xsdh', title: '销售单号', align: 'center', width: 50},
            {field: 'xslx', title: '销售类型', align: 'center', width: 30},
            {field: 'fhsj', title: '发货日期', align: 'center', width: 35},
            {field: 'sxsj', title: '制单日期', align: 'center', width: 35},
            {field: 'kh_jc', title: '客户名称', align: 'center', width: 50},
            {field: 'xsjs', title: '销售件数', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }
            },
            {field: 'xssl', title: '销售重量', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }
            },
            {field: 'xsje', title: '销售金额(元)', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatJeForHtml2(value, ",");
                    return sl;
                }
            },
            {field: 'fyzr_z', title: '配送类型', align: 'center', width: 25},
            {field: 'sbr_xm', title: '申报人', align: 'center', hidden: true},
            {field: 'orderId', title: '来源类型', align: 'center', width: 25, formatter: function (value) {
                if (value)
                    return "线上";
                return "线下";
            }},
            {field: 'xsgs_jc', title: '销售公司', align: 'center', width: 30},
            {field: 'sbgs_jc', title: '申报公司', align: 'center', width: 25},
            {field: 'sbbm_jc', title: '申报部门', align: 'center', hidden: true},
            {field: 'sbr_xm', title: '申报人', align: 'center', hidden: true},
            {field: 'zt', title: '状态', width: 20, align: 'center', formatter: function (value, rowData) {
                if (value == 'ybc')
                    return '已保存';
                if (value == 'ytj')
                    return '已提交';
                if (value == 'ysp')
                    return '已审批';
            }},
            {field: 'CZZT', title: '状态', width: 10,align: 'center',formatter: function (value, row, index) {
                if (value == 'wdr')
                    return "未导入";
                if (value == 'drcg')
                    return "导入成功";
                if (value == 'drsb')
                    return "导入失败";
                if (value == 'sccg')
                    return "删除成功";
                if (value == 'scsb')
                    return "删除失败";
                return "";
            }}
        ]
    ]}
    options = $.extend(options, columns);
    return options;


}